package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Camera;
import com.grad.why.studentsystem.domain.current.CurrentCaremaInfo;
import com.grad.why.studentsystem.domain.current.CurrentChooseCourseInfo;
import com.grad.why.studentsystem.service.CameraService;
import com.grad.why.studentsystem.service.OptionparamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class CameraController {
    @Autowired
    private CameraService cameraService;

    @Autowired
    private OptionparamsService optionparamsService;

    @RequestMapping("/getCameraInfos")
    @ResponseBody
    public Object getCameraInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<CurrentCaremaInfo> rows = cameraService.getCameraInfos(offset, limit, search);
            long total = cameraService.getCameraInfosLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addCameraInfo")
    @ResponseBody
    public String addCameraInfo(Camera camera){
        try{
            int cameraid = cameraService.getMaxCameraId() + 1;
            camera.setCameraid(cameraid);
            int n = cameraService.addCameraInfo(camera);
            if(n>0){
                return "OK";
            }else{
                return null;
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteCameraInfo/{ids}")
    @ResponseBody
    public String deleteCameraInfo(@PathVariable("ids")String ids){
        try{
            String[] idArray = ids.split("-");
            for(String s : idArray){
                int n = cameraService.deleteCameraInfoByCameraId(Integer.parseInt(s));
                if(n <= 0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateCameraInfo")
    @ResponseBody
    public String updateCameraInfo(Camera camera){
        try{
            camera.setShouldupdate(1);
            int n = cameraService.updateCameraInfo(camera);
            if(n>0){
                return "OK";
            }
            return null;
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadCameraInfos")
    @ResponseBody
    public String uploadCameraInfos(@RequestParam("file") MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadCameraInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadCameraInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = cameraService.addCameraInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
